import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_book/constant/color.dart';
import 'package:flutter_book/constant/style.dart';
import 'package:flutter_book/util/screen_util.dart';
import 'package:get/get.dart';

// ignore: must_be_immutable
class ButtonCommonSwitch extends StatelessWidget {
  final Function? onTap;
  final String title;
  final bool isShowSwitch;
  final bool isOpen;
  late ButtonCommonSwitchOpenController _openController;

  ButtonCommonSwitch(this.title, {this.onTap, this.isShowSwitch = false, this.isOpen = false}) {
    _openController = ButtonCommonSwitchOpenController(isOpen);
  }

  @override
  Widget build(BuildContext context) {
    return Material(
      color: AppColor.white,
      child: new InkWell(
        child: _switchBtn(
          title,
          isShowSwitchBtn: isShowSwitch,
          isOpen: isOpen,
          onChanged: isShowSwitch
              ? (bool) {
                  _openController.setIsOpen(bool);
                  if (onTap != null) {
                    onTap!(title, bool);
                  }
                }
              : null,
        ),
        splashColor: AppColor.textHint,
        onTap: () {
          if (isShowSwitch) {
            if (onTap != null) {
              onTap!(!_openController.isOpen.value);
            }
            _openController.setIsOpen(!_openController.isOpen.value);
          } else {
            if (onTap != null) {
              onTap!(title);
            }
          }
        },
      ),
    );
  }

  Widget _switchBtn(String title, {bool isShowSwitchBtn = false, bool isOpen = false, Function(bool)? onChanged}) {
    return Container(
      height: 48,
      width: ScreenUtil.instance.width,
      padding: const EdgeInsets.symmetric(horizontal: 16),
      child: Center(
        child: Row(
          children: [
            Text(
              title,
              style: AppStyle.textRegular16,
            ),
            Expanded(child: SizedBox()),
            Visibility(
              visible: isShowSwitchBtn,
              child: Transform.scale(
                scale: 0.75,
                child: Obx(
                  () => CupertinoSwitch(
                    activeColor: AppColor.mainRed,
                    value: _openController.isOpen.value,
                    onChanged: (bool value) {
                      if (onChanged != null) {
                        onChanged(value);
                      }
                    },
                  ),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

class ButtonCommonSwitchOpenController extends GetxController {
  var isOpen = false.obs;

  ButtonCommonSwitchOpenController(bool isOpen) {
    this.isOpen.value = isOpen;
  }

  setIsOpen(bool isOpen) => this.isOpen.value = isOpen;
}

class TextButtonCommon extends StatelessWidget {
  final EdgeInsets? padding;
  final String text;
  final TextStyle? style;
  final Function()? onTap;

  TextButtonCommon(this.text, {this.padding, this.onTap, this.style});

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () {
        if (onTap != null) {
          onTap!();
        }
      },
      child: Container(
        padding: padding,
        child: Text(
          text,
          style: style,
        ),
      ),
    );
  }
}

class TextButtonStyle extends StatelessWidget {
  //值
  final String text;

  //点击
  final Function()? onTap;

  //长按
  final Function()? onLongPress;

  //双击
  final Function()? onDoubleTap;

  //默认的padding-left:16,top:6,right:16,bottom:6
  //当有width 时 top和bottom 默认为0
  //当有height 时 left和right 默认为0
  final EdgeInsets? padding;

  //默认true
  final bool isHavePadding;

  //自定义padding时使用
  final double? leftPadding;
  final double? rightPadding;
  final double? topPadding;
  final double? bottomPadding;

  //默认没有margin
  final EdgeInsets? margin;

  //自定义margin时使用
  final double? leftMargin;
  final double? rightMargin;
  final double? topMargin;
  final double? bottomMargin;

  //按钮背景色-默认没有
  final Color? backgroundColor;

  //位置--默认居中
  final AlignmentGeometry? alignment;

  //按钮外观样式-默认没有
  final Decoration? decoration;

  //是不是圆形
  final bool isCircle;
  final double? allRadius;
  final double? topRadius;
  final double? bottomRadius;
  final double? topLeftRadius;
  final double? topRightRadius;
  final double? bottomLeftRadius;
  final double? bottomRightRadius;
  final double? borderWidth;
  final Color? borderColor;

  //宽高
  final double? width;
  final double? height;

  //文字样式
  final TextStyle? textStyle;
  final Color? textColor;
  final double? textSize;

  TextButtonStyle(
    this.text, {
    this.padding,
    this.isHavePadding = true,
    this.leftPadding,
    this.rightPadding,
    this.topPadding,
    this.bottomPadding,
    this.onTap,
    this.onLongPress,
    this.onDoubleTap,
    this.margin,
    this.leftMargin,
    this.rightMargin,
    this.topMargin,
    this.bottomMargin,
    this.backgroundColor,
    this.alignment=Alignment.center,
    this.decoration,
    this.isCircle = false,
    this.allRadius,
    this.topRadius,
    this.bottomRadius,
    this.topLeftRadius,
    this.topRightRadius,
    this.bottomLeftRadius,
    this.bottomRightRadius,
    this.borderWidth,
    this.borderColor,
    this.width,
    this.height,
    this.textStyle,
    this.textColor,
    this.textSize,
  })  : assert(
          backgroundColor == null || decoration == null,
          '背景色(backgroundColor)和样式(decoration)不能同时存在\n'
          '请使用："decoration: BoxDecoration(color: backgroundColor)".',
        ),
        assert(
          (borderWidth == null && borderColor == null) || (borderWidth != null && borderColor != null),
          '边框宽度(borderWidth)和边框颜色(borderColor)要么都有,要么都没有,不能一个有一个没有',
        ),
        assert(
          (borderWidth == null && borderColor == null) || decoration == null,
          '边框宽度(borderWidth)和边框颜色(borderColor)不能和样式(decoration)同时存在\n'
          '请使用："decoration: BoxDecoration(border: Border.all(width: borderWidth, color: borderColor))".',
        ),
        assert(
          (textColor == null && textSize == null) || textStyle == null,
          '文字大小(textSize)和文字颜色(textColor)不能和文字样式(textStyle)同时存在\n'
          '请使用："textStyle: TextStyle(fontSize: textSize,color: textColor)".',
        );

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: onTap,
      onLongPress: onLongPress,
      onDoubleTap: onDoubleTap,
      child: Container(
        width: width,
        height: height,
        padding: _getPadding(),
        margin: _getMargin(),
        color: _getBackgroundColor(),
        decoration: _getDecoration(),
        alignment: alignment,
        child: Text(
          text,
          style: _getTextStyle(),
        ),
      ),
    );
  }

  //获取padding
  EdgeInsets? _getPadding() {
    if (!isHavePadding) {
      return null;
    }
    if (padding != null) {
      return padding;
    }
    if (leftPadding != null || rightPadding != null || bottomPadding != null || topPadding != null) {
      return EdgeInsets.only(
          left: width != null ? 0 : leftPadding ?? 16,
          top: height != null ? 0 : topPadding ?? 6,
          right: width != null ? 0 : rightPadding ?? 16,
          bottom: height != null ? 0 : bottomPadding ?? 6);
    }
    return const EdgeInsets.symmetric(horizontal: 16, vertical: 6);
  }

  //获取margin
  EdgeInsets? _getMargin() {
    if (margin != null) {
      return margin;
    }
    if (leftMargin != null || rightMargin != null || topMargin != null || bottomMargin != null) {
      return EdgeInsets.only(
          left: leftMargin ?? 0, top: topMargin ?? 0, right: rightMargin ?? 0, bottom: bottomMargin ?? 0);
    }
    return null;
  }

  //获取背景色
  Color? _getBackgroundColor() {
    if (backgroundColor == null) {
      return null;
    }
    if (decoration != null) {
      return null;
    }
    if (isCircle ||
        borderWidth != null ||
        borderColor != null ||
        allRadius != null ||
        topRadius != null ||
        bottomRadius != null ||
        topLeftRadius != null ||
        topRightRadius != null ||
        bottomRightRadius != null ||
        bottomLeftRadius != null) {
      return null;
    }
    return backgroundColor;
  }

  //获取外观样式decoration
  Decoration? _getDecoration() {
    if (decoration != null) {
      return decoration;
    }
    if (isCircle ||
        borderWidth != null ||
        borderColor != null ||
        allRadius != null ||
        topRadius != null ||
        bottomRadius != null ||
        topLeftRadius != null ||
        topRightRadius != null ||
        bottomRightRadius != null ||
        bottomLeftRadius != null) {
      return BoxDecoration(
        borderRadius: isCircle
            ? BorderRadius.circular(1000)
            : allRadius != null
                ? BorderRadius.circular(allRadius ?? 0)
                : BorderRadius.only(
                    topLeft: Radius.circular(topRadius ?? topLeftRadius ?? 0),
                    topRight: Radius.circular(topRadius ?? topRightRadius ?? 0),
                    bottomRight: Radius.circular(bottomRadius ?? bottomRightRadius ?? 0),
                    bottomLeft: Radius.circular(bottomRadius ?? bottomLeftRadius ?? 0),
                  ),
        color: backgroundColor,
        border: borderWidth != null
            ? Border.all(width: borderWidth ?? 0, color: borderColor ?? AppColor.transparent)
            : null,
      );
    }
    return null;
  }

  //获取文字样式
  TextStyle? _getTextStyle() {
    if (textStyle != null) {
      return textStyle;
    }
    if (textSize != null || textColor != null) {
      return TextStyle(fontSize: textSize, color: textColor);
    }
    return null;
  }
}
